﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Game_Admin_Demo.admin
{
    public partial class editGame : System.Web.UI.Page
    {
        DataClasses1DataContext dc = new DataClasses1DataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int masp = Convert.ToInt32(Request.QueryString["id"]);
               
                var sp = dc.sanphams.SingleOrDefault(z=>z.masp==masp);
                TextBoxName.Text = sp.tensp;
                TextBoxQuantity.Text = sp.soluong.ToString();
                TextBoxPrice.Text = sp.gia.ToString();
                TextBoxImage.Text = sp.hinhanh;
                TextBoxDate.Text = sp.ngaysx.ToString();
                CheckBox1.Checked =(bool) sp.tinhtrang;
                TextBoxDescription.Text = sp.mota;

                var listDm = dc.danhmucs;
                var dm = dc.danhmucs.SingleOrDefault(z=>z.madm==sp.madm);
                DropDownList1.DataSource = listDm;
                DropDownList1.DataTextField = "tendm";
                DropDownList1.DataValueField = "madm";
                DropDownList1.DataBind();
                DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText(dm.tendm));
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                int masp = Convert.ToInt32(Request.QueryString["id"]);

                var sp = dc.sanphams.SingleOrDefault(z => z.masp == masp);

                sp.tensp =Server.HtmlEncode(TextBoxName.Text);
                sp.soluong = Convert.ToInt32(TextBoxQuantity.Text);
                sp.gia = Convert.ToDecimal(TextBoxPrice.Text);
                sp.hinhanh = TextBoxImage.Text;
                sp.ngaysx = Convert.ToDateTime(TextBoxDate.Text.ToString());
              
                sp.tinhtrang = CheckBox1.Checked;
                sp.mota = TextBoxDescription.Text;
                sp.madm = Convert.ToInt32(DropDownList1.SelectedValue);
              
                dc.SubmitChanges();
                Server.Transfer("Games.aspx");
               
             
            }
        }

        protected void TextBoxDate_TextChanged(object sender, EventArgs e)
        {
           
        }
    }
}